<?php

namespace Addons\UserCenter\Model;

use Home\Model\WeixinModel;

/**
 * UserCenter的微信模型
 */
class WeixinAddonModel extends WeixinModel {
	var $config = array ();
	function reply($dataArr, $keywordArr = array()) {
        $config = getAddonConfig ( 'UserCenter' ); // 获取后台插件的配置参数
        $map = [
            'token' => get_token(),
            'openid' => get_openid()
        ];
        $followInfo = M('Follow')->where($map)->find();
        $map['timestamp'] = time();
        switch($followInfo['status']){
            case '0'://未关注
                $articl = [
                    'Title' => '微官网',
                    'Description' => '',
                    'PicUrl' => empty($followInfo['headimgurl'])? $config['baseImg']: $followInfo['headimgurl'],
                    'Url' => addons_url('WeiSite://WeiSite/index', $map),
                ];
                break;
            case '1'://已关注
                $articl = [
                    'Title' => '请先绑定账号',
                    'Description' => '',
                    'PicUrl' => empty($followInfo['headimgurl'])? $config['baseImg']: $followInfo['headimgurl'],
                    'Url' => addons_url('UserCenter://UserCenter/userCenter',$map),
                ];
                break;
            case '2'://已绑定
                $articl = [
                    'Title' => '会员中心',
                    'Description' => '',
                    'PicUrl' => empty($followInfo['headimgurl'])? $config['baseImg']: $followInfo['headimgurl'],
                    'Url' => addons_url('UserCenter://UserCenter/userCenter',$map),
                ];
                break;
            case '3'://会员卡成员
                $articl = [
                    'Title' => '会员中心',
                    'Description' => '',
                    'PicUrl' => empty($followInfo['headimgurl'])? $config['baseImg']: $followInfo['headimgurl'],
                    'Url' => addons_url('UserCenter://UserCenter/userCenter', $map),
                ];
                break;
            default://处理未知参数
                $articl = [
                    'Title' => '微官网'.$map['openid'],
                    'Description' => '',
                    'PicUrl' => empty($followInfo['headimgurl'])? $config['baseImg']: $followInfo['headimgurl'],
                    'Url' => addons_url('WeiSite://WeiSite/index', $map),
                ];
                break;
        }
        $this->replyNews([$articl]);
	}
	// 关注时的操作
	function subscribe($dataArr) {
		$info = D ( 'Common/Follow' )->init_follow ( $dataArr ['FromUserName'] );
		
		// 增加积分
		session ( 'mid', $info ['id'] );
		add_credit ( 'subscribe' );
	}
	// 取消关注公众号事件
	public function unsubscribe() {
		// 增加积分
		add_credit ( 'unsubscribe' );
        M('Fllow')->where(['openid'=>get_openid(),'token'=>get_token()])->save(['status' => 0]);
	}
}
        	